package com.qwert.shortlink.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qwert.shortlink.project.dao.entity.ShortLink;
import com.qwert.shortlink.project.dao.mapper.ShortLinkMapper;
import com.qwert.shortlink.project.dto.req.recyclebin.RecycleBinSaveReqDTO;
import com.qwert.shortlink.project.service.IRecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.qwert.shortlink.project.common.constant.RedisKeyConstant.ROUTER_SHORT_LINK_KEY;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2024/7/6 20:42
 **/
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper, ShortLink> implements IRecycleBinService {
    private final StringRedisTemplate stringRedisTemplate;
    @Override
    public Integer saveRecycleBin(RecycleBinSaveReqDTO recycleBinSaveReqDTO) {
        //
        String fullShortUrl = recycleBinSaveReqDTO.getFullShortUrl();
        LambdaUpdateWrapper<ShortLink> updateWrapper = Wrappers.lambdaUpdate(ShortLink.class)
                .eq(ShortLink::getFullShortUrl, recycleBinSaveReqDTO.getFullShortUrl())
                .eq(ShortLink::getGid, recycleBinSaveReqDTO.getGid())
                .eq(ShortLink::getEnableStatus, 0)
                .eq(ShortLink::getDelFlag, 0);
        ShortLink shortLink = ShortLink.builder()
                .enableStatus(1)
                .build();
        int updated = baseMapper.update(shortLink, updateWrapper);
        // 清除缓存
        stringRedisTemplate.delete(String.format(ROUTER_SHORT_LINK_KEY, fullShortUrl));
        return updated;
    }
}
